import logging
import sys
import os
from logging.handlers import RotatingFileHandler
import io
import streamlit as st
from datetime import datetime
@st.cache_resource
def get_logger(log_type):
    # 创建一个新的输出流，并指定编码为UTF-8
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

    # 获取当前日期和时间
    now = datetime.now()
    # 格式化日期为"YYYY-MM-DD"
    tag = now.strftime("%Y-%m-%d")
    tag = log_type + tag
    accPath = 'logs'
    if not os.path.exists(accPath):
        os.makedirs(accPath, 0o644)
    logger = logging.getLogger(tag)
    logger.setLevel(logging.DEBUG)
    fh = RotatingFileHandler(accPath + os.sep + tag + '.log', maxBytes=1024 * 1024 * 100, backupCount=30,
                             encoding='utf-8')
    fh.setLevel(logging.DEBUG)
    ch = logging.StreamHandler(sys.stdout)
    ch.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(asctime)s - %(thread)d - %(levelname)s - %(message)s")
    ch.setFormatter(formatter)
    fh.setFormatter(formatter)
    logger.addHandler(ch)
    logger.addHandler(fh)
    return logger
